//
//  Ext+UIButton.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit
import SDWebImage

//创建Label分类
extension UILabel {
    
    /// 快速创建Label
    ///
    /// - parameter textColor: 文字颜色
    /// - parameter fontSize:  字体大小
    /// - parameter text:      文字内容
    ///
    /// - returns: 
    convenience init(textColor: UIColor, fontSize: CGFloat, text: String, maxWidth: CGFloat = 0) {
        self.init()
        self.textColor = textColor
        self.font = UIFont.systemFont(ofSize: fontSize)
        self.text = text
        if maxWidth > 0 {
            
            //指定最大显示的宽度
            self.preferredMaxLayoutWidth = maxWidth
            
            self.numberOfLines = 0
        }
    }
}

//创建imageView分类
extension UIImageView {
    
    /// 快速创建imageView
    ///
    /// - parameter imageName: 图片名称
    ///
    /// - returns: 
    convenience init(imageName: String) {
        self.init(image: UIImage(named: imageName))
    }
    
    //对SDWebImage 进行封装
    
    func CZ_setImage(withUrlString: String?, placeholderImageName: String?){
        
        //获取图片的url
        let url = URL(string: withUrlString ?? "")
        
        //判断是否为nil
        guard let u = url else {
            return
        }
        //调用SDWebImage的方法
        self.sd_setImage(with: u, placeholderImage: UIImage(named: placeholderImageName ?? ""))
    }
}


//button按钮的分类
extension UIButton {
    
    /// 创建button快捷键
    ///
    /// - parameter imageName:       按钮图片
    /// - parameter backgroundImage: 背景图片
    /// - parameter target:          target
    /// - parameter action:          action
    ///
    /// - returns:
    convenience init(imageName: String, backgroundImage: String, target: Any?, action: Selector) {
        self.init()
        //设置按钮图片
        self.setImage(UIImage(named:imageName), for: UIControlState.normal)
        self.setImage(UIImage(named:"\(imageName)_highlighted"), for: UIControlState.highlighted)
        //设置背景图片
        self.setBackgroundImage(UIImage(named:backgroundImage), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named:"\(backgroundImage)_highlighted"), for: UIControlState.highlighted)
        //监听方法
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        //设置button的大小
        self.sizeToFit()
    }
    
    /// 创建带有title的button的快捷键
    ///
    /// - parameter highlightedImgName:    图片
    /// - parameter title:                 标题
    /// - parameter target:                target
    /// - parameter action:                action
    ///
    /// - returns:
    convenience init(highlightedImgName: String? = nil, title:  String? = nil, target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        // 设置图片
        if let img = highlightedImgName {
            // 设置 image
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        //设置标题和颜色字体大小
        if let tit = title {
            
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(CZTHEMECOLOR, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        
        self.sizeToFit()
    }
    
    /// 实例化一个有背景图片和文字的 button
    ///
    /// - parameter setBackgroundImgName:
    /// - parameter title:                文字
    /// - parameter fontSize:             文字大小
    /// - parameter titleColor:           文字颜色
    /// - parameter target:
    /// - parameter action:
    ///
    /// - returns: 
    convenience init(setBackgroundImgName: String, title:  String, fontSize: CGFloat, titleColor: UIColor,  target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.setBackgroundImage(UIImage(named: setBackgroundImgName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        self.sizeToFit()
    }

}

